package com.fortune.service.impl;

import com.fortune.dao.MStoreDeficitDao;
import com.fortune.dao.MStoreSurplusDao;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitAddDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitInfoDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitListDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusAddDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusEditDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusInfoDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusListDto;
import com.fortune.exception.db.DeleteInnerErrorException;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.MStoreDeficitService;
import com.fortune.service.MStoreSurplusService;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfoDetail;
import com.fortune.vo.mStoreSurplus.MStoreSurplusInfo;
import com.fortune.vo.mStoreSurplus.MStoreSurplusInfoDetail;
import com.fortune.vo.mStoreSurplus.StockDetailSurplus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

@Service
public class MStoreSurplusServiceImpl implements MStoreSurplusService {
    @Autowired
    private MStoreSurplusDao mStoreSurplusDao;


    @Override
    @Transactional
    public int addSurplusBase(String companyId, String uid, MStoreSurplusAddDto addDto) {
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addSurplusBase=mStoreSurplusDao.addSurplusBase(companyId,uuid,uid,addDto);
        if(addSurplusBase>0){
            int addSurplusDetail=0;
            for(MStoreSurplusInfoDto infoDto: addDto.getmStoreSurplusInfoDto()){
                addSurplusDetail=mStoreSurplusDao.addSurplusDetail(companyId,uuid,infoDto);
                if(addSurplusDetail<1){
                    throw new InsertInnerErrorException("新增报盈商品失败");
                }
            }
            return addSurplusDetail;
        }
        return addSurplusBase;
    }

    @Override
    public List<MStoreSurplusInfo> findSurplusList(String companyId, MStoreSurplusListDto mStoreSurplusListDto) {
        SimpleDateFormat date =new SimpleDateFormat("yyyy-MM-dd");
        List<MStoreSurplusInfo> findSurplusList=mStoreSurplusDao.findSurplusList(companyId,mStoreSurplusListDto);
        for(MStoreSurplusInfo mStoreSurplusInfo:findSurplusList){
            if(mStoreSurplusInfo.getCreateTime()!=null){
                mStoreSurplusInfo.setCreatTimes(date.format(mStoreSurplusInfo.getCreateTime()));
            }
            if(mStoreSurplusInfo.getInTime()!=null){
                mStoreSurplusInfo.setInTimes(date.format(mStoreSurplusInfo.getInTime()));
            }
        }
        return findSurplusList;
    }

    @Override
    public int findSurplusListCount(String companyId, MStoreSurplusListDto mStoreSurplusListDto) {
        return mStoreSurplusDao.findSurplusListCount(companyId,mStoreSurplusListDto);
    }

    @Override
    public MStoreSurplusInfoDetail findDetail(String companyId, String deficitId) {
        return mStoreSurplusDao.findSurplusInfo(companyId,deficitId);
    }

    @Override
    @Transactional
    public int editMStoreSurplusConfirm(String companyId, String uuid,String operatorId) {
        int addSurplusBase = mStoreSurplusDao.editMStoreSurplusConfirm(companyId, uuid,operatorId);
        int no = 0;
        float kk;
        float rainNum;
        if(addSurplusBase>0){
            MStoreSurplusAddDto list = mStoreSurplusDao.findData(companyId,uuid);
            int success = 0;
            for(MStoreSurplusInfoDto infoDto: list.getmStoreSurplusInfoDto()){
                    StockDetailSurplus num = mStoreSurplusDao.findProductStock(companyId,list.getStoreId(),infoDto.getProductId());
                    if(num == null){
                        no = 1;
                        rainNum = infoDto.getNum().floatValue();
                    }else{
                        no = num.getNo() + 1;
                        kk = infoDto.getNum().floatValue();
                        rainNum = kk + num.getRemainNum();
                    }
                    success = mStoreSurplusDao.addProDuctStock(companyId,list.getStoreId(),infoDto.getProductId(),no,uuid,infoDto.getNo(),infoDto.getNum(),rainNum);
                    if(success < 1){
                        throw new InsertInnerErrorException("新增库存明细账失败");
                    }
            }
            return success;
        }else{
            throw new InsertInnerErrorException("新增库存明细账失败");
        }
    }

    @Override
    public int deleteDStoreSurplus(String companyId, String uuid) {
        return mStoreSurplusDao.deleteMStoreSurplus(companyId, uuid);
    }

    @Override
    public int editSurplusBase(String companyId, MStoreSurplusEditDto editDto) {
        int editSurplusBase=mStoreSurplusDao.editSurplusBase(companyId,editDto);
        if(editSurplusBase>0){
            String uuid=editDto.getUuid();
            int addSurplusDetail=0;
            int deleteInfo=mStoreSurplusDao.deleteInfo(companyId,uuid);
            if(deleteInfo>0){
                for(MStoreSurplusInfoDto infoDto: editDto.getmStoreDeficitInfoDto()){
                    addSurplusDetail=mStoreSurplusDao.addSurplusDetail(companyId,uuid,infoDto);
                    if(addSurplusDetail<1){
                        throw new InsertInnerErrorException("新增报盈商品失败");
                    }
                }
            }
            return addSurplusDetail;
        }
        return editSurplusBase;
    }


}
